package com.busionline.wotu.baselibrary.navigation.bottom;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author woochen123
 * @time 2017/11/22 10:26
 * @desc
 */
public abstract class TabItemView<T extends TabItemView.Builder> {

    protected View mTabItemView;
    protected T build;

    protected TabItemView(T builder) {
        this.build = builder;
    }

    /**
     * 得到条目
     *
     * @return
     */
    public View getTabView() {
        if (build.layoutId == 0) {
            throw new IllegalArgumentException("please invoke setContentView first");
        }
        if (mTabItemView == null) {
            mTabItemView = LayoutInflater.from(build.context).inflate(build.layoutId, null);
            initView();
        }
        return mTabItemView;
    }

    protected void setText(int layoutId, String msg) {
        TextView view = getView(layoutId);
        view.setText(msg);
    }

    protected void setImageRes(int layoutId, int resId) {
        ImageView view = getView(layoutId);
        view.setImageResource(resId);
    }

    /**
     * 布局初始化
     */
    protected abstract void initView();

    /**
     * 是否选择当前条目
     * @param selected
     */
    public abstract void setSelected(boolean selected);


    /**
     * 获得资源id的View
     */
    protected <T extends View> T getView(int layoutId) {
        return (T) mTabItemView.findViewById(layoutId);
    }

    public abstract static class Builder {
        protected Context context;
        protected int layoutId;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder(Context context,int layoutId) {
            this.context = context;
            this.layoutId = layoutId;
        }

        public Builder setContentView(int layoutId) {
            this.layoutId = layoutId;
            return this;
        }

        public abstract <T extends TabItemView>T build();


    }
}
